//
//  LSBaseViewController.m
//  GenericProject
//
//  Created by 漠然丶情到深处 on 2018/5/30.
//  Copyright © 2018年 漠然丶情到深处. All rights reserved.
//

#import "LSBaseViewController.h"

@interface LSBaseViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation LSBaseViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    self.isHidenNaviBar = YES;
    
//    NSDictionary *dic = @{NSForegroundColorAttributeName : LSRedColor,
//                                 NSFontAttributeName : [UIFont systemFontOfSize:18]};
        
//           if (@available(iOS 13.0, *)) {
//               UINavigationBarAppearance *barApp = [UINavigationBarAppearance new];
//               barApp.backgroundColor = LSWhiteColor;
//               #//基于backgroundColor或backgroundImage的磨砂效果
//               barApp.backgroundEffect = nil;
//               #//阴影颜色（底部分割线），当shadowImage为nil时，直接使用此颜色为阴影色。如果此属性为nil或clearColor（需要显式设置），则不显示阴影。
//               barApp.shadowColor = LSWhiteColor;
//               //标题文字颜色
////               barApp.titleTextAttributes = dic;
//               self.navigationController.navigationBar.scrollEdgeAppearance = barApp;
//               self.navigationController.navigationBar.standardAppearance = barApp;
//           }else {
////              self.navigationController.navigationBar.titleTextAttributes = dic;
//               [self.navigationController.navigationBar setShadowImage:[UIImage new]];
////              UIImage *navBgImg = [[UIImage ls_imageWithColor:LSRedColor size:CGSizeMake(LSScreenWidth, 44.f)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//              [self.navigationController.navigationBar setBackgroundImage:[UIImage ls_imageWithColor:LSWhiteColor] forBarMetrics:UIBarMetricsDefault];
//           }
//        //透明设置
//        self.navigationController.navigationBar.translucent = NO;
        //navigationItem控件的颜色
//        self.navigationController.navigationBar.tintColor = [UIColor redColor];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    self.isHidenNaviBar = NO;
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lsLigthColor:ViewBackgroundColor darkColor:LSBlackColor];
    // Do any additional setup after loading the view.
}

//隐藏系统导航栏
- (void)setIsHidenNaviBar:(BOOL)isHidenNaviBar {
    _isHidenNaviBar = isHidenNaviBar;
    if (isHidenNaviBar) {
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    }else{
        [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
}

//请求数据
- (void)analysis {
    
}

//创建布局
- (void)createSubViews {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/** 该方法在UIView的分类中实现 */
- (BOOL)isShowingOnKeyWindow {
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.view.frame fromView:self.view.superview];
    CGRect winBounds = keyWindow.bounds;
    
    // 主窗口的bounds 和 self的矩形框 是否有重叠
    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
    
    return !self.view.isHidden && self.view.alpha > 0.01 && self.view.window == keyWindow && intersects;
}

- (UITableView *)mainTableView {
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, LSScreenHeight - [UIDevice ls_dev_tabBarFullHeight]) style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = [UIColor lsLigthColor:ViewBackgroundColor darkColor:LSBlackColor];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.estimatedRowHeight = 0;
        _mainTableView.estimatedSectionHeaderHeight = 0;
        _mainTableView.estimatedSectionFooterHeight = 0;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        // 获取系统左滑手势
        for (UIGestureRecognizer *ges in _mainTableView.gestureRecognizers) {
            if ([ges isKindOfClass:NSClassFromString(@"_UISwipeActionPanGestureRecognizer")]) {
                [ges addTarget:self action:@selector(_swipeRecognizerDidRecognize:)];
            }
        }
        
//        if (@available(iOS 11.0, *)) {
//            _mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        } else {
//            self.automaticallyAdjustsScrollViewInsets = NO;
//        }
    }
    return _mainTableView;
}

//禁止tableView左滑删除时，按住左滑再向右滑动时有弹簧效果的问题
- (void)_swipeRecognizerDidRecognize:(UISwipeGestureRecognizer *)swip {
    CGPoint currentPoint = [swip locationInView:self.mainTableView];
    for (UITableViewCell *cell in self.mainTableView.visibleCells) {
        if (CGRectContainsPoint(cell.frame, currentPoint)) {
            if (cell.frame.origin.x > 0) {
                cell.frame = CGRectMake(0, cell.frame.origin.y,cell.bounds.size.width, cell.bounds.size.height);
            }
        }
    }
}

//动态更新状态栏颜色
- (void)setStatusBarStyle:(UIStatusBarStyle)StatusBarStyle {
    _StatusBarStyle = StatusBarStyle;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return _StatusBarStyle;
}

#pragma mark - UITableViewDelegate/UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return LSSYRealValue(50);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionHeaderView = [[UIView alloc]init];
    
    return sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *sectionFotterView = [[UIView alloc]init];
    
    return sectionFotterView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
